﻿#include "sqlitebasic.h"

SqliteBasic::SqliteBasic(QObject *parent, const QString &databaseName) : QObject(parent)
{
    if (QSqlDatabase::contains(databaseName)) {
        database = QSqlDatabase::database(databaseName);
    } else {
        database = QSqlDatabase::addDatabase("QSQLITE");

        database.setDatabaseName(databaseName);
    }
}

bool SqliteBasic::openDb()
{
    if (!database.open()) {
        qDebug() << database.lastError().text();
    } else {
        qDebug() << "open success";
        return true;
    }
}

bool SqliteBasic::execute(const QString &sql)
{
    QSqlQuery q;

    return q.exec(sql);
}


QSqlQuery SqliteBasic::query(const QString &sql)
{
    QSqlQuery q;

    q.exec(sql);

    return q;
}



void SqliteBasic::closeDb()
{
    database.close();
    qDebug() << "close";
}
